package com.qian.controller;

import com.qian.pojo.Employees;
import com.qian.service.EmployeeService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/emp")
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;

    //登录模块
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResponseEntity login(@RequestBody Map map, HttpSession session) {
        String employeeAccount = (String) map.get("employeeAccount");
        String employeePassword = (String) map.get("employeePassword");
        Map<String, Object> res = new HashMap<String, Object>();
        List<Employees> list = employeeService.queryEmployeeByAccount(employeeAccount);
        if (list.size() == 0) {
            res.put("msg", 501);
        } else {
            if (!list.get(0).getEmployeePassword().equals(employeePassword)) {
                res.put("msg", 502);
            } else {
                Employees old = list.get(0);
                //将租户信息加入到session中，方便之后的相关操作进行过滤
                session.setAttribute("emp",old);
                session.setAttribute("tenantId", old.getTenantId());
                session.setAttribute("employeeId", old.getEmployeeId());
                session.setAttribute("employeeCompany", old.getEmployeeCompany());
                res.put("msg", 200);
            }
        }
        return new ResponseEntity(res, HttpStatus.OK);
    }

    //模糊查询与全查询
    @RequestMapping(value = "/searchByName", method = RequestMethod.POST)
    public ResponseEntity searchAll(@RequestBody Map map) {
        String employeeName = (String) map.get("employeeName");
        List<Employees> list = employeeService.queryEmployeeByName(employeeName);
        //雪花算法不匹配前端js的最大值，对溢出情况进行处理
        List<String> empId = new ArrayList();
        List<String> empName = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            empId.add(list.get(i).getEmployeeId().toString());
            empName.add(list.get(i).getEmployeeName());
        }
        HashMap res = new HashMap();
        res.put("data", list);
        res.put("empId", empId);
        res.put("empName",empName);
        return new ResponseEntity(res, HttpStatus.OK);
    }


    //获取当前用户的session
    @RequestMapping("/getSession")
    public ResponseEntity getSession(HttpSession session) {
        Map<String, Object> res = new HashMap();
        res.put("emp", session.getAttribute("emp"));
        return new ResponseEntity(res, HttpStatus.OK);
    }

    //注销
    @RequestMapping("/logout")
    public void logout(HttpSession session) {
        session.removeAttribute("emp");
        session.removeAttribute("tenantId");
        session.removeAttribute("employeeId");
        session.removeAttribute("employeeCompany");
    }

    //获取信息
    @RequestMapping(value = "/getInfo",method = RequestMethod.GET)
    public ResponseEntity getInfo(HttpSession session) {
        Employees emp = employeeService.selectEmployeeById((Long) session.getAttribute("employeeId"));
        HashMap res = new HashMap();
        res.put("emp",emp);
        return new ResponseEntity(res,HttpStatus.OK);
    }

    //修改密码
    @RequestMapping(value = "/updatePsd", method = RequestMethod.POST)
    public ResponseEntity updatePsd(@RequestBody Map map, HttpSession session) {
        String password = (String) map.get("employeePassword");
        Long employeeId = (Long) session.getAttribute("employeeId");
        Employees employees = new Employees();
        employees.setEmployeeId(employeeId);
        employees.setEmployeePassword(password);
        employeeService.updateEmployee(employees);
        HashMap res = new HashMap();
        res.put("msg", "密码修改成功！");
        return new ResponseEntity(res, HttpStatus.OK);
    }

    //增加用户
    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    public ResponseEntity addUser(@RequestBody Map map, HttpSession session) {
        Employees employees = new Employees();
        employees.setEmployeeAccount((String) map.get("employeeAccount"));
        employees.setEmployeePassword((String) map.get("employeePassword"));
        employees.setEmployeeName((String) map.get("employeeName"));
        employees.setEmployeeGender((String) map.get("employeeGender"));
        employees.setEmployeeCompany((String) session.getAttribute("employeeCompany"));
        employees.setEmployeeEmail((String) map.get("employeeEmail"));
        employees.setEmployeeAddress((String) map.get("employeeAddress"));
        employees.setEmployeePosition((String) map.get("employeePosition"));
        employees.setAuthority(0);
        employeeService.addEmployee(employees);
        HashMap res = new HashMap();
        res.put("msg", "添加成功");
        return new ResponseEntity(res, HttpStatus.OK);
    }

    //删除用户
    @RequestMapping(value = "/deleteUser", method = RequestMethod.POST)
    public ResponseEntity deleteUser(@RequestBody Map map) {
        Long employeeId = Long.parseLong((String) map.get("employeeId"));
        employeeService.deleteEmployee(employeeId);
        HashMap res = new HashMap();
        res.put("msg", "删除成功");
        return new ResponseEntity(res, HttpStatus.OK);
    }

    //更新用户
    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    public ResponseEntity updateUser(@RequestBody Map map) {
        Employees employees = new Employees();
        employees.setEmployeeId(Long.parseLong((String) map.get("employeeId")));
        employees.setEmployeeAccount((String) map.get("employeeAccount"));
        employees.setEmployeePassword((String) map.get("employeePassword"));
        employees.setEmployeeName((String) map.get("employeeName"));
        employees.setEmployeeGender((String) map.get("employeeGender"));
        employees.setEmployeeEmail((String) map.get("employeeEmail"));
        employees.setEmployeeAddress((String) map.get("employeeAddress"));
        employees.setEmployeePosition((String) map.get("employeePosition"));
        employees.setAuthority(Integer.parseInt(map.get("authority") + ""));
        employeeService.updateEmployee(employees);
        HashMap res = new HashMap();
        res.put("msg", "更新成功");
        return new ResponseEntity(res, HttpStatus.OK);
    }

    //根据id查询用户
    @RequestMapping(value = "/queryUserById", method = RequestMethod.POST)
    public ResponseEntity queryTenantById(@RequestBody Map map) {
        HashMap res = new HashMap();
        Employees employee = employeeService.selectEmployeeById(Long.parseLong((String) map.get("employeeId")));
        res.put("employee", employee);
        return new ResponseEntity(res, HttpStatus.OK);
    }

}
